package nature.nano.control.script.run;

import java.io.File;
import java.io.IOException;

import nature.nano.component.Strings;
import nature.nano.control.script.parse.StringHelper;
import nature.nano.environment.Config;

public class ScriptRunner {

	public static void primary(String Provider) {
		MetaRunner.run(
			Provider + "(P)",
			"EnvironmentChecker(E)",
			"InlineCommentFilter(I)",
			"VariableParser(V)",
			"ScriptLineParser(S)",
			"ErrorHandleChecker(C)",
			"ScriptAssembly(A)",
			"FinishedMessager(M)",
			"StaticErrors(T)",
			"P > I > V > S > C > A",
			"P.error > ErrorNormalizer(PE) > M",
			"E.error > ErrorNormalizer(EE) > M",
			"T.error > ErrorNormalizer(TE) > M",
			"I.error > ErrorNormalizer(IE) > M",
			"V.error > ErrorNormalizer(VE) > M",
			"S.error > ErrorNormalizer(SE) > M",
			"A.error > ErrorNormalizer(AE) > M",
			"A > TimeCounter > M",
			"PE > Controller",
			"EE > Controller",
			"TE > Controller",
			"IE > Controller",
			"VE > Controller",
			"SE > Controller",
			"AE > Controller",
			"M > Printer[format=half]");
	}

	public static void run(String script) {
		primary("ScriptInputter[script=" + script + "]");
	}
	
	public static void run(String... scripts) {
		run(StringHelper.mergeScripts(scripts));
	}

	public static void runFile(String file) throws IOException {
		primary("ScriptFileReader[file=" + file + "]");
	}
	
	public static void main(String[] args) throws IOException {
		if (args.length == 0) {
			System.out.println("no args");
			return;
		}
		String fileName = args[0];
		File file = new File(fileName);
		if (!file.exists()) {
			String path = Config.current().getProp(Strings.defaultScriptFolder) + "/" + fileName;
			file = new File(Config.current().getPath(path));
		}
		if (file.exists()) {
			ScriptRunner.runFile(file.getAbsolutePath());			
		} else {
			System.out.println("can't load script file");
		}
	}

}
